package org.example.flink

import org.apache.flink.streaming.api.scala._
object Hello04WordCountByDataStreamUseScala {
  def main(args: Array[String]): Unit = {
    //1.创建执行环境（上下文）
    val environment: StreamExecutionEnvironment =
      StreamExecutionEnvironment.getExecutionEnvironment
    //2.调用Source方法创建DataStream
    val lineStream: DataStream[String] =
      environment.socketTextStream("localhost", 19523)
//    //3.调用Transformation(s)
//    val wordStream: DataStream[String] = lineStream.flatMap(_.split(" "))
//    val mapStream: DataStream[(String, Int)] = wordStream.map((_, 1))
//    //分区聚合
//    val keyByStream: KeyedStream[(String, Int), String] =
//      mapStream.keyBy(_._1)
//    val sumStream: DataStream[(String, Int)] = keyByStream.sum(1)
//
//    //4.调用Sink
//    sumStream.print()


    //调用Transformation(s)
    lineStream.flatMap(_.split(" ")).map((_, 1)).keyBy(_._1).sum(1).print()

    //5.启动执行
    environment.execute()
  }

}
